<% add_decidim_page_title(t(".title")) %>

<section id="components">
  <div class="item_show__header">
    <h1 class="item_show__header-title">
      <%= t(".title") %>
    </h1>
  </div>

  <%= decidim_form_for @permissions_form, url: url_for(action: :update, **resource_params), html: { class: "form form-defaults" }, method: "put" do |permissions_form| %>
    <div class="form__wrapper">
      <%= permissions_form.fields_for :permissions, permissions_form.object do |action_permission_form| %>
        <% action_permission_form.object.permissions.each do |action, permission| %>
          <fieldset class="card <%= action %>-permission">
            <% if @component %>
              <div class="card-divider"><%= t("#{@component.manifest.name}.actions.#{action}", scope: "decidim.components") %></div>
            <% else %>
              <div class="card-divider"><%= t("#{resource.resource_manifest.name}.actions.#{action}", scope: "decidim.resources") %></div>
            <% end %>

            <div class="card-section p-4">
              <%= action_permission_form.fields_for(action, permission) do |permission_form| %>
                <%= permission_form.collection_check_boxes :authorization_handlers,
                                                           authorizations,
                                                           :name,
                                                           :description do |b| %>
                  <div class="row column">
                    <%= b.label { b.check_box + b.text } %>
                    <% if permission.authorization_handlers_names.include? b.value %>
                      <div id="authorization-handler-<%= b.value %>" class="authorization-handler">
                        <%= render "options_form", form: permission_form, handler_name: b.value, ephemeral_warning: b.object.ephemeral? %>
                      </div>
                    <% else %>
                      <% missing_permission = Decidim::Admin::PermissionForm.new(authorization_handlers: { b.value => {} }) %>
                      <%= action_permission_form.fields_for(action, missing_permission) do |missing_permission_form| %>
                        <div id="authorization-handler-<%= b.value %>" class="authorization-handler" style="display:none">
                          <%= render "options_form", form: missing_permission_form, handler_name: b.value, ephemeral_warning: b.object.ephemeral? %>
                        </div>
                      <% end %>
                    <% end %>
                  </div>
                <% end %>
              <% end %>
            </div>
          </fieldset>
        <% end %>
      <% end %>
      <div class="item__edit-sticky">
        <div class="item__edit-sticky-container">
          <%= permissions_form.submit t(".submit"), class: "button button__sm button__secondary" %>
        </div>
      </div>
    </div>
  <% end %>
</section>
